package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"guest/models"
	"math"
)

type GuestController struct {
	beego.Controller
}

type JsonRe struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type JsonR struct {
	Code    int         `json:"code"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
	Page    int         `json:"page"`
	Size    int         `json:"size"`
	Count   int64       `json:"count"`
	Sumpage int         `json:"sumpage"`
}

func (c *GuestController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	var count int64
	models.DB.Model(&models.Guset{}).Count(&count)
	sumpage := int(math.Ceil(float64(count) / float64(size)))
	offset := (page - 1) * size
	var guset []models.Guset
	err := models.DB.Offset(offset).Limit(size).Find(&guset).Error
	if err != nil {
		res := JsonRe{
			Code: 0,
			Msg:  "数据查询失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code:    200,
			Msg:     "数据查询成功",
			Data:    guset,
			Page:    page,
			Size:    size,
			Count:   count,
			Sumpage: sumpage,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}
